package com.ebt.m.customer.util;

import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.net.URLDecoder;

/**
 * Created by Rick.Wang on 2017/1/19.
 */

public class AttachmentUtil {

    public static String netPathToLocalPath(String netPath, String localPathDir){
        if (netPath == null){
            return null;
        }
        //网络路径，如果本地存在文件，转换为本地路径
        if (netPath.startsWith("http")){
            String[] paths = netPath.split("/");
            if (paths != null && paths.length > 0){
                String fileName = paths[paths.length - 1];
                String subFileName = urlDecode(fileName);
                if (TextUtils.isEmpty(subFileName)){
                    return netPath;
                }
                //根据此subFileName去附件文件夹下寻找文件，如果存在，就返回此path
                File file = new File(localPathDir + subFileName);
                if (file.exists()){
                    return file.getAbsolutePath();
                }else{
                    return netPath;
                }
            }else{
                //有问题
                Log.d("atta","pathToLocalPath path : " + netPath);
                return netPath;
            }
        }else{
            //本地路径
            return netPath;
        }
    }

    public static String urlDecode(String url){
        String ret = null;
        try {
            ret = URLDecoder.decode(url,"UTF-8");
        }catch (Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    public static String netPathToLocalPathWithoutCareExist(String netPath, String localPathDir){
        if (netPath == null){
            return null;
        }
        //网络路径，如果本地存在文件，转换为本地路径
        if (netPath.startsWith("http")){
            String[] paths = netPath.split("/");
            if (paths != null && paths.length > 0){
                String fileName = paths[paths.length - 1];
                String subFileName = urlDecode(fileName);
                //根据此subFileName去附件文件夹下寻找文件，如果存在，就返回此path
                File file = new File(localPathDir + subFileName);
                new File(localPathDir).mkdirs();
                return file.getAbsolutePath();
            }else{
                //有问题
                Log.d("atta","pathToLocalPath path : " + netPath);
                return netPath;
            }
        }else{
            //本地路径
            return netPath;
        }
    }

    public static boolean isPathFromNet(String path){
        if (TextUtils.isEmpty(path)){
            return false;
        }
        return path.startsWith("http");
    }

    public static String getLastFileNameByUrl(String url){
        String fileName = "";
        if (isPathFromNet(url)){
            String[] paths = url.split("/");
            if (paths != null && paths.length > 0) {
                fileName = paths[paths.length - 1];
            }
        }else{
            String[] paths = url.split(File.separator);
            if (paths != null && paths.length > 0) {
                fileName = paths[paths.length - 1];
            }
        }
        return fileName;
    }
}
